package DBLayer;
import ModelLayer.AttributeGroup;
import java.sql.*;
import java.util.ArrayList;

public class DBAttributeGroup {
    // <editor-fold defaultstate="collapsed" desc="Attributes & Constructors">
    private Connection conn;

    public DBAttributeGroup() {
        conn = SQLConnector.getInstance().getConnection();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL SELECT Methods">
    public AttributeGroup getAttributeGroupByID(int id) {
        return selectSingle("SELECT `id_attribute_group`, `is_color_group`, `name`, `public_name` FROM `ps_attribute_group` LEFT JOIN `ps_attribute_group_lang` USING (`id_attribute_group`) WHERE (`id_lang` = " + Constants.DEFAULT_LANGUAGE + " OR `id_lang` IS NULL) AND `id_attribute_group` = " + id + " LIMIT 0, 1;");
    }

    public ArrayList<AttributeGroup> getAttributeGroupList() {
        return selectMultiple("SELECT `id_attribute_group`, `is_color_group`, `name`, `public_name` FROM `ps_attribute_group` LEFT JOIN `ps_attribute_group_lang` USING (`id_attribute_group`) WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " OR `id_lang` IS NULL ORDER BY `id_attribute_group` ASC;");
    }

    private AttributeGroup selectSingle(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        AttributeGroup attributeGroup = new AttributeGroup();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            if (result.next()) {
                attributeGroup = buildAttributeGroup(result);
            }
            System.out.println("Fetching AttributeGroup Object");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return attributeGroup;
    }

    private ArrayList<AttributeGroup> selectMultiple(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        ArrayList<AttributeGroup> attributeGroups = new ArrayList<AttributeGroup>();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            while (result.next()) {
                AttributeGroup attributeGroup = new AttributeGroup();
                attributeGroup = buildAttributeGroup(result);
                attributeGroups.add(attributeGroup);
            }
            System.out.println("Fetching AttributeGroup List");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return attributeGroups;
    }

    private AttributeGroup buildAttributeGroup(ResultSet result) {
        AttributeGroup attributeGroup = new AttributeGroup();
        try {
            attributeGroup.setIDAttributeGroup(result.getInt("id_attribute_group"));
            if (result.getInt("is_color_group") == 1) {
                attributeGroup.setIsColorGroup(true);
            } else {
                attributeGroup.setIsColorGroup(false);
            }
            attributeGroup.setName(result.getString("name"));
            attributeGroup.setPublicName(result.getString("public_name"));

        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
        return attributeGroup;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL INSERT, UPDATE & DELETE Methods">
    public int addAttributeGroup(AttributeGroup attributeGroup) {
        int returnID = 0;
        Statement stmt = null;
        ResultSet result = null;

        String query = "INSERT INTO `ps_attribute_group_lang` (`id_attribute_group`, `id_lang`, `name`, `public_name`) "
                + "VALUES (" + attributeGroup.getIDAttributeGroup() + ", " + Constants.DEFAULT_LANGUAGE + ", '" + attributeGroup.getName() + "', '" + attributeGroup.getPublicName() + "');";

        query += "INSERT INTO `ps_attribute_group` (`id_attribute_group`, `is_color_group`) "
                + "VALUES (" + attributeGroup.getIDAttributeGroup() + ", ";
        if (attributeGroup.getIsColorGroup() == true)   { query += " 1);"; }
        else                                            { query += " 0);"; }

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            result = stmt.getGeneratedKeys();
            if (result.next()) {
                returnID = result.getInt(1);
            }
            System.out.println("Added AttributeGroup ID " + returnID);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public int updateAttributeGroup(AttributeGroup attributeGroup, int id) {
        int returnID = 0;
        Statement stmt = null;
	String query = "UPDATE `ps_attribute_group` SET "
                + "`id_attribute_group` = "  + attributeGroup.getIDAttributeGroup() + ", ";
        if (attributeGroup.getIsColorGroup() == true)   { query += "`is_color_group` = 1 "; }
        else                                            { query += "`is_color_group` = 0 "; }
        query += "WHERE `id_attribute_group` = " + id + " LIMIT 1; ";

        query += "UPDATE `ps_attribute_group_lang` SET "
                + "`id_attribute_group` = "  + attributeGroup.getIDAttributeGroup() + ", "
                + "`name`               = "  + attributeGroup.getName() + ", "
                + "`public_name`        = "  + attributeGroup.getPublicName() + " "
                + "WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " AND `id_attribute_group` = " + id + " LIMIT 1;";

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query);
            returnID = attributeGroup.getIDAttributeGroup();
            System.out.println("Updated AttributeGroup ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public void deleteAttributeGroup(int id) {
        Statement stmt = null;

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate("DELETE FROM `ps_attribute_group` WHERE `id_attribute_group` = " + id + " LIMIT 1; DELETE FROM `ps_attribute_group_lang` WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " AND `id_attribute_group` = " + id + " LIMIT 1;");
            System.out.println("Deleted AttributeGroup ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
    }
    // </editor-fold>
}
